package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.domain.HhyMember;
import com.huihuiyun.huihuiyun.server.domain.HhyWallet;
import com.huihuiyun.huihuiyun.server.service.HhyMemberService;
import com.huihuiyun.huihuiyun.server.service.HhyWalletService;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Component
@Path(Const.context)
@Slf4j
public class MemberClientWalletController {
    @Context
    private HttpServletResponse httpServletResponse;

    @Autowired
    private HhyMemberService memberService;

    @Autowired
    private HhyWalletService hhyWalletService;

    /*******************
     * wallet
     ******************/
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findWalletByMemberId")
    public HhyWallet findWalletByMemberId(@QueryParam("memberId") String memberId) {
        try {
            HhyMember member = memberService.findOne(memberId);
            String walletId = member.walletId;
            HhyWallet hhyWallet = hhyWalletService.findIf(walletId);
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/withdrawWalletByWalletIdAndWithdrawPoints")
    public HhyWallet withdrawWalletByWalletIdAndWithdrawPoints(@QueryParam("walletId") String walletId, @QueryParam("withdrawPoints") long withdrawPoints) {
        try {
            HhyWallet hhyWallet = hhyWalletService.withdrawByWalletIdAndWithdrawPoints(walletId, withdrawPoints);
            return hhyWallet;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return null;
        }
    }
}